//
//  ManageBillDetailViewController.m
//  wtshoppingcite
//
//  Created by TW on 15-4-22.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "ManageBillDetailViewController.h"
#import "BillsDetailModel.h"
#import "PaymentViewController.h"

@interface ManageBillDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)UIView *headView;
@property(nonatomic,strong)UITableView *billDetailTableView;

@end

@implementation ManageBillDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitleView:@"缴费列表"];
    
    _dataArray = [[NSMutableArray alloc]initWithCapacity:1];
    
    [self getManageFeeBillDetail];
    
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 100)];
    [self.view addSubview:_headView];
    
    UILabel *label = [self createLabelWithFrame:CGRectMake(10, 0, kWidth, 99) text:self.text font:18.0f textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
    label.numberOfLines = 0;
    label.text = self.text;
    [_headView addSubview:label];
    
    UIView *seperateLine = [[UIView alloc]initWithFrame:CGRectMake(0, 100 - 1, kWidth, 1)];
    seperateLine.backgroundColor = MAIN_GRAY_COLOR;
    [_headView addSubview:seperateLine];
    
    
    _billDetailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _headView.bottom, kWidth, kHeight-label.height-64) style:UITableViewStylePlain];
    _billDetailTableView.dataSource = self;
    _billDetailTableView.delegate = self;
    _billDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_billDetailTableView];
    
    [self initWithView];
    
    // Do any additional setup after loading the view.
}

#pragma mark 应付总价
-(void)initWithView{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight - 50 - 64, kWidth, 50)];
    [self.view addSubview:view];
    
    UILabel *totalPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,10,80, 40)];
    totalPriceLabel.text = @"应付总价:";
    [view addSubview:totalPriceLabel];
    totalPriceLabel.font = [UIFont systemFontOfSize:15.0f];
    totalPriceLabel.backgroundColor = [UIColor clearColor];
    totalPriceLabel.textColor = UIColorFromRGB(0x9fa0a0);
    
    
    UIImageView *iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(totalPriceLabel.right, 20, 10, 10)];
    iconImage.bottom = totalPriceLabel.bottom -15;
    iconImage.image = [UIImage imageNamed:@"money_code"];
    [view addSubview:iconImage];
    
    
    UILabel *totalLabel = [[UILabel alloc]initWithFrame:CGRectMake(iconImage.right, 6, 220, 40)];
    totalLabel.bottom = totalPriceLabel.bottom;
    totalLabel.left = iconImage.right;
    totalLabel.text =self.billsModel.totalCost;
    [view addSubview:totalLabel];
    totalLabel.font = [UIFont systemFontOfSize:24.0f];
    totalLabel.backgroundColor = [UIColor clearColor];
    totalLabel.textColor = MAIN_BLUE_COLOR;
    
    
    UIButton *commitButton = [[UIButton alloc]initWithFrame:CGRectMake(6, 6, 100, 40)];
    commitButton.bottom = totalLabel.bottom-5;
    commitButton.right = kWidth - 10;
    [commitButton setBackgroundImage:[UIImage imageNamed:@"register_select"] forState:UIControlStateNormal];
    [commitButton setTitle:@"提交订单" forState:UIControlStateNormal];
    [commitButton setFont:[UIFont systemFontOfSize:15]];
    [view addSubview:commitButton];
    [commitButton setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    [commitButton addTarget:self action:@selector(commitAction) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mrak 提交订单
-(void)commitAction{
    
    PaymentViewController *payMentVC = [[PaymentViewController alloc]init];
    payMentVC.hidesBottomBarWhenPushed = YES;
    payMentVC.type = NSManageFeePayForStyle;
    payMentVC.billsModel = self.billsModel;
    payMentVC.totalPrice = self.billsModel.totalCost;
    [self.navigationController pushViewController:payMentVC animated:YES];
    
}


#pragma  mark uitableview dataSource&delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"billDetailCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"billDetailCell"];
        
        UILabel *textLabel = [self createLabelWithFrame:CGRectMake(10, 10, kWidth-40, 20) text:@"" font:14 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
        textLabel.tag = 1000;
        [cell.contentView addSubview:textLabel];
        
        UILabel *moneyLabel = [self createLabelWithFrame:CGRectMake(kWidth-60 , 10, 60, 20) text:@"" font:14 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
        moneyLabel.tag = 1001;
        [cell.contentView addSubview:moneyLabel];
        
        UIView *seperateLine = [[UIView alloc]initWithFrame:CGRectMake(0, 30 - 1, kWidth, 1)];
        seperateLine.backgroundColor = MAIN_GRAY_COLOR;
        [cell.contentView addSubview:seperateLine];


    }
    BillsDetailModel *billsDetailModel = _dataArray[indexPath.row];
    
    UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:1000];
    textLabel.text = billsDetailModel.paymentProject;
    
    UILabel *moneyLabel = (UILabel *)[cell.contentView viewWithTag:1001];
    moneyLabel.text = billsDetailModel.money;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



#pragma mark 物业费清单
-(void)getManageFeeBillDetail{
    
    NSString *billId = self.billsModel.propertyOrderId;
    
    
    NSMutableDictionary *listDic = [NSMutableDictionary dictionary];
    SetValueForKey(listDic, billId, @"billId");
   
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kBillsDetail
                             andHttpMethod:@"POST"
                             andParameters:listDic
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         
                         request.requestCache = YES;
                         
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                         }else{
                             
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"成功数据:%@",responseDic);
                             
                             NSArray *list = [responseDic objectForKey:@"billItems"];
                             
                             for (NSDictionary *dic in list) {
                                 
                                 BillsDetailModel *billsModel = [[BillsDetailModel alloc]initWithBillsDetailModelList:dic];
                                 [self.dataArray addObject:billsModel];
                             }
                         }
                         
                         [_billDetailTableView reloadData];
                     }];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ;
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
